import {
  FundOutlined,
  DatabaseOutlined,
  SolutionOutlined,
} from "@ant-design/icons";
import Dashboard from "../views/Dashboard";
import CategoryList from "../views/category/List";
import CategoryPub from "../views/category/Pub";
import ArticleList from "../views/course/article-list";
import ArticlePub from "../views/course/article-pub";
import Category from "../views/course/category";
import NotFound from "../views/notFound";
import Role from "../views/system/role";
import User from "../views/system/user";
import ReduxSaga from "../views/testDemos/reduxSaga";
import BigFile from "../views/testDemos/bigFile";
import { cloneDeep } from "lodash-es";
export const mainRouter = [
  {
    key: "/admin/dashboard",
    label: "数据可视化",
    icon: <FundOutlined />,
    component: Dashboard,
    exact: true,
  },
  {
    key: "/admin/category",
    label: "分类管理",
    // hidden: true,
    icon: <DatabaseOutlined />,
    children: [
      {
        key: "/admin/category/list",
        label: "分类列表",
        component: CategoryList,
      },
      {
        key: "/admin/category/pub",
        label: "分类发布",
        component: CategoryPub,
      },
    ],
  },
  {
    key: "/admin/coures",
    label: "课程管理",
    icon: <DatabaseOutlined />,
    children: [
      {
        key: "/admin/coures/category",
        label: "课程分类",
        component: Category,
      },
      {
        key: "/admin/coures/list",
        label: "图文课程",
        component: ArticleList,
      },
      {
        key: "/admin/coures/pub",
        label: "图文发布",
        component: ArticlePub,
      },
    ],
  },
  {
    key: "/admin/system",
    label: "系统管理",
    icon: <SolutionOutlined />,
    children: [
      {
        key: "/admin/system/role",
        label: "角色管理",
        component: Role,
      },
      {
        key: "/admin/system/user",
        label: "账号管理",
        component: User,
      },
    ],
  },
  {
    key: "/admin/test",
    label: "练习demo",
    icon: <SolutionOutlined />,
    children: [
      {
        key: "/admin/test/saga",
        label: "redux-saga",
        component: ReduxSaga,
      },
      {
        key: "/admin/test/bigFile",
        label: "大文件上传",
        component: ReduxSaga,
      },
    ],
  },
];

// 路由数据重载
export const routerFn = (userInfo) => {
  const { power } = userInfo;
  let routers = [];
  function loop(arr) {
    arr.forEach((item) => {
      if (item.children) {
        loop(item.children);
      } else {
        if (power) {
          if (power.includes(item.key)) {
            routers.push(item);
          }
        }
      }
    });
  }
  loop(mainRouter);
  console.log("cccc", routers);
  return routers;
};

// 目录重载数据
export const menuFn = (userInfo) => {
  const { power } = userInfo.userInfo;
  let powerdata = cloneDeep(power);
  console.log("power2", powerdata);
  const menus = cloneDeep(mainRouter);
  function loop(arr) {
    for (let i = arr.length - 1; i >= 0; i--) {
      delete arr[i].exact;
      if (arr[i].children) {
        arr[i].children.forEach((item) => {
          if (powerdata) {
            if (powerdata.includes(item.key)) {
              if (!powerdata.includes(arr[i].key)) {
                powerdata.push(arr[i].key);
              }
            }
          }
        });
        loop(arr[i].children);
      }
      if (arr[i].hidden) {
        arr.splice(i, 1);
        continue;
      }
      if (powerdata) {
        if (!powerdata.includes(arr[i].key)) {
          arr.splice(i, 1);
          continue;
        }
      }
    }
  }
  loop(menus);
  console.log("power1", powerdata);
  return menus;
};
